import lombok.SneakyThrows;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class ClientSend implements Runnable {

    private ObjectOutputStream oos;
    private ChatClient client;
    private boolean isRuned = false;

    public ClientSend(ChatClient client){
        this.client = client;
        this.oos = client.getOos();
    }

    @SneakyThrows
    @Override
    public void run() {
        while (true){
            try {
                Scanner sc = new Scanner((System.in));
                String msg = sc.nextLine();
                oos.writeObject(msg);
                oos.flush();
            } catch (IOException e) {
//                e.printStackTrace();
                if(!isRuned) {
                    client.exit();
                    isRuned = true;
                }
            }
        }
    }
}